//
//  TopicDetailView.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/8.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "TopicDetailView.h"
#import "TopicDetailViewController.h"

@implementation TopicDetailView

- (IBAction)deleteAction:(UIButton *)sender {
    NSLog(@"点击了删除");

    if ([_delegate respondsToSelector:@selector(deleteTopicAction)]) {
        [_delegate deleteTopicAction];
    }
}
-(void)setControllersFrame{
    self.shadowView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 15);
    self.deleteBtn.frame = CGRectMake(SCREEN_WIDTH-50, self.shadowView.frame.origin.x+self.shadowView.frame.size.height+28, 20, 6);
    
    self.headImageView.frame = CGRectMake(20, self.shadowView.frame.origin.y+self.shadowView.frame.size.height +12 , 38, 38);
    self.headImageView.layer.cornerRadius =self.headImageView.frame.size.width/2;
    self.headImageView.layer.masksToBounds = YES;
    self.creatATLable.frame = CGRectMake(self.headImageView.frame.origin.x + self.headImageView.frame.size.width + 10, self.shadowView.frame.origin.y+self.shadowView.frame.size.height +15 , 100, 14);
    self.creatDateLable.frame = CGRectMake(self.creatATLable.frame.origin.x, self.creatATLable.frame.origin.y+self.creatATLable.frame.size.height+ 8 , 180, 12);
    
    self.textTV = [YYTextView new];
    self.textTV.frame = CGRectMake(20, self.titleLable.frame.origin.y+self.titleLable.frame.size.height + 9, SCREEN_WIDTH - 40, 1000);
    self.textTV.editable = NO;
    
    self.photosView = [[PhotosView alloc]initWithFrame:CGRectMake(8,200, SCREEN_WIDTH-2*8, 0)];
    
    [self addSubview:self.photosView];
    
}
-(void)showDataWithDictionry:(NSDictionary*)data{
    [self setControllersFrame];
    
    [self.headImageView sd_setImageWithURL:data[@"user"][@"avatar"]];
    self.creatATLable.text=data[@"user"][@"firstName"];
    
    self.creatDateLable.text=data[@"createdDate"];
    
    self.titleLable.text=data[@"name"];
    //计算行数
    CGFloat labelHeight = [self.titleLable sizeThatFits:CGSizeMake(self.titleLable.frame.size.width, MAXFLOAT)].height;
    NSNumber *count = @((labelHeight) / self.titleLable.font.lineHeight);
    self.titleLable.frame = CGRectMake(20, self.headImageView.frame.origin.y + self.headImageView.frame.size.height + 12, SCREEN_WIDTH - 40, (self.titleLable.frame.size.height) * ([count integerValue]));
    
    NSAttributedString *attStr = [self setLineSpacingWithControl:nil AndString:data[@"content"] AndLineSpacing:9 AndFont:15];
    self.textTV.attributedText = attStr;
    
    [self.textTV sizeToFit];
    [self addSubview:self.textTV];
}
-(void)setData:(BaseModel *)data{
    _data=data;    
    
    [self setControllersFrame];
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:data.createdUser.avatar]];
    self.creatATLable.text=data.createdUser.firstName;
    
    self.creatDateLable.text=data.createdDate;
    
    self.titleLable.text=data.name;
    //计算行数 
    CGFloat labelHeight = [self.titleLable sizeThatFits:CGSizeMake(self.titleLable.frame.size.width, MAXFLOAT)].height;
    NSNumber *count = @((labelHeight) / self.titleLable.font.lineHeight);
    self.titleLable.frame = CGRectMake(20, self.headImageView.frame.origin.y + self.headImageView.frame.size.height + 12, SCREEN_WIDTH - 40, (self.titleLable.frame.size.height) * ([count integerValue]));
    
    NSAttributedString *attStr = [self setLineSpacingWithControl:nil AndString:data.content AndLineSpacing:9 AndFont:15];
    self.textTV.attributedText = attStr;
    
    [self.textTV sizeToFit];
    [self addSubview:self.textTV];
    //设置显示图片
    if (data.imagePaths.count>0) {
        self.photosView.hidden = NO;
        NSMutableArray *arr=[data.imagePaths mutableCopy];
        [arr removeLastObject];
        self.photosView.pic_urls = arr;
        CGRect rect = self.photosView.frame;
        rect.origin.y =CGRectGetMaxY(self.textTV.frame)+8;
        rect.size.height =  data.imagesHeight;
        self.photosView.frame = rect;
    }else{
        self.photosView.hidden = YES;
    }

}
-(void)setTopicDetailViewFrame:(getHeightBlock)height{
    CGFloat h =self.textTV.frame.origin.y+self.textTV.frame.size.height;
    height(h);
}
//设置字体间距
-(NSAttributedString *)setLineSpacingWithControl:(UITextView*)textView AndString:(NSString *)string AndLineSpacing:(CGFloat)lineSpacing AndFont:(CGFloat)font{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:font],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    return [[NSAttributedString alloc] initWithString:string attributes:attributes];
}


@end
